// 此Hook的功能:发送请求
import { jsonp } from 'vue-jsonp'
import { ref, onMounted } from 'vue';
// 导入仓库
import { storeToRefs } from "pinia";
import { CStore } from '../../stores/counter'


// 导入hook
import { getWeatherData } from './useWeatherDate';

const $store = CStore()
const { currentCityDate, locationCity } = storeToRefs($store)


export const useHttp = () => {
    const key = 'OUTBZ-UTV3X-VMD4C-ZGR7M-5OG35-OPBG6'
    const myLocation = ref('')

    // const zeroData = ref({})
    onMounted(() => {
        getData(locationCity.value, currentCityDate.value)


    })
    const CLD = ref({})
    const CCD = ref({})
    // 获取本地的定位信息
    async function getData(ld, cd) {
        /*  CC-- 发送请求从腾讯地图获取定位信息    */
        let res1 = await jsonp('https://apis.map.qq.com/ws/location/v1/ip?key=' + key, { output: 'jsonp' })
        // CC-- 将获取到的定位信息存入变量myLocation
        let name = res1.result.ad_info.city.slice(0, 2)
        let adcode = res1.result.ad_info.adcode + ''
        adcode = adcode.slice(0, 4) + '00'
        let location = [res1.result.location.lng, res1.result.location.lat]

        // CC-- 将获取到的定位信息存到pinia中的locationCity变量里
        ld.name = name
        ld.adcode = adcode
        ld.location = location
        CLD.value = ld;





        /* //  获取天气数据
         if(cd.name == '') {
            getWeatherData(location)
            return
        }else {
            getWeatherData(cd.location)
        }   */
    }

    return { myLocation, CLD, CCD }
}